package main.java.com.blog.controller;

import com.alibaba.fastjson.JSON;
import com.blog.entity.Art;
import com.blog.entity.Forword;
import com.blog.service.ArtService;
import com.blog.service.ForwordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by Admin on 2017/6/12.
 */
@Controller
@RequestMapping("/forword")
public class ForwordController {
    private static Boolean b = false;
    @Autowired
    ForwordService forwordService;

    @Autowired
    ArtService artService;

    @ResponseBody
    @RequestMapping(value = "/insert", method = RequestMethod.GET, produces = "text/html;charset=UTF-8")
    public String insert(String forwordId, String forwordUser) {
        int id = Integer.parseInt(forwordId);
        Art art = artService.findById(id);

        if (forwordId.equals("") || forwordUser.equals("")) {

            return "转发的内容不能为空！";
        }

        List<Forword> forwordList = forwordService.findAllForword();
        for (int i = 0; i < forwordList.size(); i++) {
            if (forwordList.get(i).getForwordTitle().equals(art.getArtTitle()) &&
                    forwordList.get(i).getForwordUser().equals(art.getUserAccount())) {
                b = true;
                break;
            }
        }

        if (b.equals(true)) {
            return "已转发";
        } else {

            Forword forword = new Forword();
            forword.setForwordUser(forwordUser);
            forword.setForwordTitle(art.getArtTitle());
            forword.setForwordContext(art.getArtContext());
            forwordService.addForword(forword);

            Art art1 = new Art();
            art1.setUserAccount(forwordUser);
            art1.setArtTitle(art.getArtTitle());
            art1.setArtContext(art.getArtContext());
            artService.addArt(art1);
            return "转发成功！";
        }

    }

    @ResponseBody
    @RequestMapping(value = "/addPage", method = RequestMethod.GET, produces = "text/json;charset=UTF-8")
    public String addPage() {
        List<Forword> forwordList = forwordService.findAllForword();

        return JSON.toJSON(forwordList).toString();
    }


}
